package com.cloudsdo.cameratest.activity;

import android.graphics.Point;
import android.graphics.Rect;
import android.hardware.Camera;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;

import com.arcsoft.face.FaceEngine;
import com.cloudsdo.cameratest.R;
import com.cloudsdo.cameratest.activity.BaseActivity;
import com.cloudsdo.cameratest.camera.CameraHelper;
import com.cloudsdo.cameratest.camera.CameraListener;
import com.cloudsdo.cameratest.core.RecognizeService;
import com.cloudsdo.cameratest.widget.FaceRectView;

import java.util.List;

public class FeatureActivity extends BaseActivity {
    private static final String TAG = "FeatureActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_feature);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        //保持亮屏
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            WindowManager.LayoutParams attributes = getWindow().getAttributes();
            attributes.systemUiVisibility = View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
            getWindow().setAttributes(attributes);
        }

        initView();
    }
    Handler handler=new Handler();
    @Override
    protected void onResume() {
        super.onResume();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                initCamera();
            }
        },300);
    }
    /**
     * 相机预览显示的控件，可为SurfaceView或TextureView
     */
    private View previewView;
    private FaceRectView faceRectView;
    private void initView() {
        previewView = findViewById(R.id.texture_preview);
        faceRectView = findViewById(R.id.face_rect_view);
    }

    /**
     * 优先打开的摄像头，本界面主要用于单目RGB摄像头设备，因此默认打开前置
     */
    private Integer rgbCameraID = Camera.CameraInfo.CAMERA_FACING_FRONT;
    private CameraHelper cameraHelper;
    private RecognizeService recognizeService;
    private Camera.Size previewSize;
    private void initCamera() {
        DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);
        final FaceRectView.FaceListener faceListener=new FaceRectView.FaceListener() {
            @Override
            public void onDrawRect(final List<Rect> rectList) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (faceRectView == null) {
                            return;
                        }
                        faceRectView.clearFaceInfo();
                        if (rectList == null || rectList.size() == 0) {
                            return;
                        }
                        faceRectView.addFaceInfo(rectList);
                    }
                });
            }

            @Override
            public void onNoRect() {
                if (faceRectView == null) {
                    return;
                }
                faceRectView.clearFaceInfo();
            }
        };
        CameraListener cameraListener=new CameraListener() {
            @Override
            public void onCameraOpened(Camera camera, int cameraId, int displayOrientation, boolean isMirror) {
                previewSize = camera.getParameters().getPreviewSize();
                recognizeService =new RecognizeService.Builder()
                        .setContext(getContext())
                        .frThreadNum(10)
                        .setFaceListener(faceListener)
                        .build();
                Log.e(TAG," "+ recognizeService.toString());
                faceRectView.setInfo(previewSize.width, previewSize.height, previewView.getWidth(), previewView.getHeight(), displayOrientation
                        , cameraId, isMirror, true, false);
            }

            @Override
            public void onPreview(byte[] nv21, Camera camera) {
                if(recognizeService !=null){
                    recognizeService.requestFaceFeature(nv21,previewSize.width, previewSize.height, FaceEngine.CP_PAF_NV21);
                }
            }

            @Override
            public void onCameraClosed() {

            }

            @Override
            public void onCameraError(Exception e) {

            }

            @Override
            public void onCameraConfigurationChanged(int cameraID, int displayOrientation) {

            }
        };

        cameraHelper = new CameraHelper.Builder()
                .previewViewSize(new Point(previewView.getMeasuredWidth(), previewView.getMeasuredHeight()))
                .rotation(getWindowManager().getDefaultDisplay().getRotation())
                .specificCameraId(rgbCameraID != null ? rgbCameraID : Camera.CameraInfo.CAMERA_FACING_FRONT)
                .isMirror(false)
                .previewOn(previewView)
                .cameraListener(cameraListener)
                .build();
        cameraHelper.init();
        cameraHelper.start();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (cameraHelper != null) {
            cameraHelper.release();
            cameraHelper = null;
        }
    }

    public void open(View view) {
        initCamera();
    }

}
